package com.nl.controller.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.model.OperatorLog;
import com.nl.controller.web.bean.TestRedis;
import com.nl.controller.web.bean.TestTotal;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.mapper.TestMapper;
import com.nl.controller.web.mapper.UtilMapper;
import com.nl.controller.web.redis.RedisCluster;
import com.sun.tools.extcheck.Main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestService {

    @Autowired
    private TestMapper testMapper;
    @Autowired
    private UtilMapper utilMapper;
    @SystemServiceLog(description = "测试插入日志数据")
    public void test() {
        System.out.println("test");
    }
    
    @SystemServiceLog(value = false, description = "测试分页查询日志")
    public List<OperatorLog> queryLog(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<OperatorLog> list = testMapper.queryOperatorLogList();
        PageInfo<OperatorLog> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        System.out.println("共有商品信息：" + total);
        return pageInfo.getList();
    }
    
    
    
    /*public Map<String, String> testRedis(){
    	RedisCluster redisCluster = null;
    	Map<String, String> map = new HashMap<String, String>();
    	try {
			redisCluster=new RedisCluster(GlobalConst.REDIS_CONFIG);
			List<TestRedis> list = utilMapper.testRedis();
	    	for (TestRedis testRedis : list) {
	    		String mgrId = testRedis.getMgrId();
	    		String nowCount = testRedis.getCount();
	    		String dayCount = redisCluster.get(mgrId+"dayCount");
	    		Integer c = Integer.valueOf(nowCount) - Integer.valueOf(dayCount);
	    		redisCluster.set(mgrId+"dayCount", nowCount);
	    		redisCluster.set(mgrId+"weekCount", nowCount);
	    		redisCluster.set(mgrId+"monthCount", (Integer.valueOf(redisCluster.get(mgrId+"monthCount"))+c)+"");
	    		redisCluster.set(mgrId+"quarterCount", (Integer.valueOf(redisCluster.get(mgrId+"quarterCount"))+c)+"");
	    		redisCluster.set(mgrId+"yearCount", (Integer.valueOf(redisCluster.get(mgrId+"yearCount"))+c)+"");
	    		redisCluster.set(mgrId+"totalCount", (Integer.valueOf(redisCluster.get(mgrId+"totalCount"))+c)+"");
			}
	    	
	    	map.put("reCode", "0");
	    	map.put("count", list.size()+"");
	    	redisCluster.close();
	    	
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return map;
    }*/
    
    public Map<String, String> testRedis(){
    	RedisCluster redisCluster = null;
    	Map<String, String> map = new HashMap<String, String>();
    	try {
			redisCluster=new RedisCluster(GlobalConst.REDIS_CONFIG);
			List<TestTotal> total = utilMapper.testTotal();
			for (TestTotal testTotal : total) {
				String mgrId = testTotal.getId();
				redisCluster.set(mgrId+"monthCount", testTotal.getMonth());
	    		redisCluster.set(mgrId+"quarterCount", testTotal.getQuarter());
	    		redisCluster.set(mgrId+"yearCount", testTotal.getYear());
	    		redisCluster.set(mgrId+"totalCount", testTotal.getTotal());
			}
			List<TestRedis> list = utilMapper.testRedis();
	    	for (TestRedis testRedis : list) {
	    		String mgrId = testRedis.getMgrId();
	    		Integer addCount = Integer.valueOf(testRedis.getCount());
	    		redisCluster.set(mgrId+"monthCount", (Integer.valueOf(redisCluster.get(mgrId+"monthCount"))+addCount)+"");
	    		redisCluster.set(mgrId+"quarterCount", (Integer.valueOf(redisCluster.get(mgrId+"quarterCount"))+addCount)+"");
	    		redisCluster.set(mgrId+"yearCount", (Integer.valueOf(redisCluster.get(mgrId+"yearCount"))+addCount)+"");
	    		redisCluster.set(mgrId+"totalCount", (Integer.valueOf(redisCluster.get(mgrId+"totalCount"))+addCount)+"");
			}
	    	
	    	map.put("reCode", "0");
	    	map.put("count", list.size()+"");
	    	redisCluster.close();
	    	
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return map;
    }
}
